#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
单例模式(Singleton Pattern)是一个常用的软件设计模式，该模式的主要目的是确保某一个类只有一个实例存在，当希望在某个系统中只出现一个实例时，单例对象就能派上用场。
"""

import functools


def Singleton(cls):
    _instance = {}

    @functools.wraps(cls)
    def inner(*args, **kwargs):
        if cls not in _instance:
            _instance[cls] = cls(*args, **kwargs)
        return _instance[cls]

    return inner


class SingleTest:
    __instance = None

    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            cls.__instance = object.__new__(cls)
        return cls.__instance

    def __str__(self):
        print('sleeping...')


@Singleton
class Config:
    def __init__(self):
        self.config = [1, 2, 3, 4, 5]

    def get_config(self):
        return self.config


if __name__ == '__main__':
    config = Config()
    print(config.get_config())
